package cn.cyh.queue_service.controller;

import cn.cyh.queue_service.common.Result;
import cn.cyh.queue_service.common.ResultCode;
import cn.cyh.queue_service.model.dto.PageRequest;
import cn.cyh.queue_service.model.dto.PageResult;
import cn.cyh.queue_service.model.entity.Notice;
import cn.cyh.queue_service.model.vo.NoticeVO;
import cn.cyh.queue_service.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 通知公告控制器
 */
@RestController
@RequestMapping("/notices")
@Tag(name = "通知公告", description = "通知公告相关接口")
@Slf4j
@RequiredArgsConstructor
public class NoticeController {

    private final NoticeService noticeService;
    
    /**
     * 分页获取通知公告列表
     *
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 通知公告列表分页结果
     */
    @GetMapping
    @Operation(summary = "分页获取通知公告列表", description = "分页获取通知公告列表")
    public Result<PageResult<NoticeVO>> getNoticeList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "排序字段") @RequestParam(required = false) String orderBy,
            @Parameter(description = "是否升序") @RequestParam(required = false, defaultValue = "false") boolean isAsc) {
        
        log.info("获取通知公告列表: pageNum={}, pageSize={}", pageNum, pageSize);
        
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        pageRequest.setOrderBy(orderBy);
        pageRequest.setIsDesc(!isAsc);
        
        PageResult<NoticeVO> pageResult = noticeService.getNoticeList(pageRequest);
        return Result.success(pageResult);
    }
    
    /**
     * 获取当前有效的通知公告列表
     *
     * @return 通知公告列表
     */
    @GetMapping("/active")
    @Operation(summary = "获取当前有效的通知公告列表", description = "获取当前有效的通知公告列表")
    public Result<List<NoticeVO>> getActiveNotices() {
        log.info("获取当前有效的通知公告列表");
        List<NoticeVO> noticeList = noticeService.getActiveNotices();
        return Result.success(noticeList);
    }
    
    /**
     * 获取首页展示的通知公告列表
     *
     * @return 通知公告列表
     */
    @GetMapping("/home")
    @Operation(summary = "获取首页展示的通知公告列表", description = "获取首页展示的通知公告列表")
    public Result<List<NoticeVO>> getHomePageNotices() {
        log.info("获取首页展示的通知公告列表");
        List<NoticeVO> noticeList = noticeService.getHomePageNotices();
        return Result.success(noticeList);
    }
    
    /**
     * 根据ID获取通知公告详情
     *
     * @param id 通知公告ID
     * @return 通知公告详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取通知公告详情", description = "根据通知公告ID获取通知公告详情")
    public Result<NoticeVO> getNoticeById(@Parameter(description = "通知公告ID") @PathVariable Long id) {
        log.info("获取通知公告详情: id={}", id);
        NoticeVO noticeVO = noticeService.getNoticeById(id);
        return Result.success(noticeVO);
    }
    
    /**
     * 创建通知公告
     *
     * @param notice 通知公告实体
     * @return 新创建的通知公告ID
     */
    @PostMapping
    @Operation(summary = "创建通知公告", description = "创建新的通知公告")
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPERADMIN')")
    public Result<Long> createNotice(@Valid @RequestBody Notice notice) {
        log.info("创建通知公告: {}", notice.getTitle());
        Long id = noticeService.createNotice(notice);
        return Result.success(id);
    }
    
    /**
     * 更新通知公告
     *
     * @param id 通知公告ID
     * @param notice 通知公告实体
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新通知公告", description = "根据通知公告ID更新通知公告")
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPERADMIN')")
    public Result<Boolean> updateNotice(
            @Parameter(description = "通知公告ID") @PathVariable Long id,
            @Valid @RequestBody Notice notice) {
        
        log.info("更新通知公告: id={}", id);
        boolean result = noticeService.updateNotice(id, notice);
        return Result.success(result);
    }
    
    /**
     * 删除通知公告
     *
     * @param id 通知公告ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除通知公告", description = "根据通知公告ID删除通知公告")
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPERADMIN')")
    public Result<Boolean> deleteNotice(@Parameter(description = "通知公告ID") @PathVariable Long id) {
        log.info("删除通知公告: id={}", id);
        boolean result = noticeService.deleteNotice(id);
        return Result.success(result);
    }
    
    /**
     * 更新通知公告状态
     *
     * @param id 通知公告ID
     * @param status 状态
     * @return 更新结果
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新通知公告状态", description = "根据通知公告ID更新通知公告状态")
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPERADMIN')")
    public Result<Boolean> updateStatus(
            @Parameter(description = "通知公告ID") @PathVariable Long id,
            @Parameter(description = "状态：0-不显示, 1-显示") @RequestParam Integer status) {
        
        log.info("更新通知公告状态: id={}, status={}", id, status);
        
        if (status != 0 && status != 1) {
            return Result.failed(ResultCode.PARAM_ERROR, "状态值只能为0或1");
        }
        
        boolean result = noticeService.updateStatus(id, status);
        return Result.success(result);
    }
} 